import * as EnumApi from "@/api/enum";
import * as MyApplyApi from "@/api/myApply";
import * as ResourceTypeFieldApi from "@/api/resourceTypeField";
import * as ResourceTypeEntityApi from "@/api/resourceTypeEntity";
import * as ResourceTypeEntityDataApi from "@/api/resourceTypeEntityData";
import { applyStatus } from "@/config/settings";
import { mapGetters } from "vuex";

import ApplyInfoFormComponent from "@/views/pages/components/ApplyInfoForm";
import ApplyFreeEntityTableComponent from "@/views/pages/components/ApplyFreeEntityTable";

export default {
    data() {
        return {
            // 申请信息列表
            applyInfoList: [],
            // 流程图获取地址
            processImageURL: "",
            // 申请状态
            applyStatus: {},

            // 是否展示申请列表
            showApplyList: true,

            // 分页设置
            layout: "total, sizes, prev, pager, next, jumper",
            pageData: {
                currentPage: 1,
                pageSize: 20,
                total: 0,
            },

            // 控制添加申请窗口显示与消失
            addApplyDialogVisible: false,
            //当前是添加还是修改 true:添加;false:修改
            addOrUpdate: true,
            // 申请信息
            applyInfo: {},

            // 资源类别下动态字段信息
            resourceTypeEntityFieldList: [],
        };
    },

    components: { ApplyInfoFormComponent, ApplyFreeEntityTableComponent },

    methods: {
        handleApplyFreeEntity() {
            this.showApplyList = false;
        },

        handleShowApplyList() {
            this.showApplyList = true;
            this.getApplyInfoList();
        },

        // 通过当前登录用户获取申请信息列表
        getApplyInfoList() {
            MyApplyApi.getApplyListInfoByApplyBy(this.userInfo.username).then(
                (res) => {
                    this.applyInfoList = res.data;
                    this.applyInfoList.forEach((applyInfoItem) => {
                        this.handleExpandChange(applyInfoItem, "");
                    });
                }
            );
        },

    // 申请单展开触发事件
    handleExpandRow(row, expandedRows) {
      this.$refs.applyInfoList.toggleRowExpansion(row);
    },
    handleExpandChange(row, expandedRows) {
      if (!row.items && row.applyResourceId == 0) {
        this.getResourceCacheInfoById(
          row.resourceCacheInfoId,
          row.resourceTypeName
        );
      } else if (!row.items && row.applyResourceId != 0) {
        this.getResourceEntityInfoById(
          row.applyResourceId,
          row.resourceTypeName
        );
      }
    },
    // 通过缓存资源id获取缓存资源信息
    getResourceCacheInfoById(resourceCacheInfoId, resourceTypeName) {
      let self = this;
      MyApplyApi.getResourceCacheInfoById(resourceCacheInfoId).then((res) => {
        const index = self.applyInfoList.findIndex(
          (data) => data.resourceCacheInfoId === resourceCacheInfoId
        );
        ResourceTypeFieldApi.getResourceTypeFieldByName(resourceTypeName).then(
          (res) => {
            self.$set(self.applyInfoList[index], "entityFieldInfo", res.data);
          }
        );
        self.$set(
          self.applyInfoList[index],
          "items",
          JSON.parse(res.data.resourceEntityInfo)
        );
      });
    },
    // 通过资源实体id获取资源信息
    getResourceEntityInfoById(resourceTypeEntityId, resourceTypeName) {
      let self = this;
      ResourceTypeEntityDataApi.getResourceTypeEntityDataByEntityId(
        resourceTypeEntityId
      ).then((res) => {
        const index = self.applyInfoList.findIndex(
          (data) =>
            data["applyResourceId"] == resourceTypeEntityId && !data["items"]
        );
        ResourceTypeFieldApi.getResourceTypeFieldByName(resourceTypeName).then(
          (res) => {
            self.$set(self.applyInfoList[index], "entityFieldInfo", res.data);
          }
        );
        let entityFieldInfo = {};
        res.data.forEach((resourceEntityFieldValue) => {
          self.$set(
            entityFieldInfo,
            resourceEntityFieldValue.resourceTypeFieldName,
            resourceEntityFieldValue.resourceTypeEntityDataValue
          );
        });
        self.$set(self.applyInfoList[index], "items", entityFieldInfo);
      });
    },
        // 提交申请
        handleSubmitApply(index, row) {
            MyApplyApi.submitApply(row.id).then((res) => {
                this.showProcessMessage(res);
                if (res.code == 200) {
                    if (row.applyResourceId != 0) {
                        let param = {
                            resourceTypeEntityId: row.applyResourceId,
                            status: "apply",
                        };
                        ResourceTypeEntityApi.updateResourceTypeEntityStatusById(
                            param
                        ).then((res) => {});
                    }
                }
            });
        },
        // 删除申请
        handleDeleteApply(index, row) {
            MyApplyApi.remarkDeleteApplyInfoById(row.id).then((res) => {
                this.showProcessMessage(res);
            });
        },
        // 撤销申请
        handleCancelApply(index, row) {
            MyApplyApi.cancelApply(row.id).then((res) => {
                this.showProcessMessage(res);
                if (res.code == 200) {
                    if (row.applyResourceId != 0) {
                        let param = {
                            resourceTypeEntityId: row.applyResourceId,
                            status: "free",
                        };
                        ResourceTypeEntityApi.updateResourceTypeEntityStatusById(
                            param
                        ).then((res) => {});
                    }
                }
            });
        },
        // 归还资源
        handleReturnResource(index, row) {
            MyApplyApi.returnResource(row.id).then((res) => {
                this.showProcessMessage(res);
            });
        },
        // 展示流程处理结果信息
        showProcessMessage(res) {
            if (res.code == 200) {
                this.$message.success(res.message);
                this.getApplyInfoList();
            } else {
                this.$message.error(res.message);
            }
        },

        // 添加申请信息
        handleAddApplyInfoDialog() {
            this.addApplyDialogVisible = true;
        },
        // 修改申请信息
        handleEditInfoDialog(index, row) {
            this.addApplyDialogVisible = true;
            this.addOrUpdate = false;

            this.applyInfo = row;
            this.$set(this.applyInfo, "applyTime", [
                row.applyStartUseTime,
                row.applyEndUseTime,
            ]);
        },
        // 弹窗关闭
        closedDialog(dialogVisible) {
            this.addApplyDialogVisible = dialogVisible;
            this.addOrUpdate = true;
            this.getApplyInfoList();
        },

        // 获取实体数据列表数据
        getResourceTypeEntityDataList() {
            ResourceTypeEntityApi.getResourceTypeEntityListByName(
                this.applyInfo.resourceTypeName
            ).then((res) => {
                this.resourceTypeEntityDataList = res.data;
                // 获取资源实体动态字段数据
                this.getResourceTypeEntityFieldData();
            });
        },
        // 获取资源实体动态字段数据
        getResourceTypeEntityFieldData() {
            this.resourceTypeEntityDataList.forEach((element) => {
                ResourceTypeEntityDataApi.getResourceTypeEntityDataByEntityId(
                    element.resourceTypeEntityId
                ).then((res) => {
                    res.data.forEach((value) => {
                        this.$set(
                            element,
                            value.resourceTypeFieldName,
                            value.resourceTypeEntityDataValue
                        );
                    });
                });
            });
        },

        // 获取资源类别实体字段列表
        getResourceTypeEntityFieldList() {
            ResourceTypeFieldApi.getResourceTypeFieldByName(
                this.applyInfo.resourceTypeName
            ).then((res) => {
                this.resourceTypeEntityFieldList = res.data;
                this.resourceTypeEntityFieldList.forEach((fieldInfo) => {
                    if (fieldInfo.resourceTypeFieldType == "enum") {
                        EnumApi.getEnumItemByGroupName(fieldInfo.enumGroupName).then(
                            (res) => {
                                this.$set(fieldInfo, "enumItemList", res.data);
                            }
                        );
                    }
                });
            });
        },

        // 当前页发生改变
        handleCurrentChange(val) {
            this.pageData.currentPage = val;
        },
        // 当前页大小发生改变
        handleSizeChange(val) {
            this.pageData.pageSize = val;
        },

        // 表格样式设置
        getHeadClass({ rowIndex }) {
            if (rowIndex == 0) {
                return "background-color:#ccc;color:#fff;font-size:30px;";
            }
        },
        getRowClassName({ row, rowIndex }) {
            if (rowIndex % 2 !== 0) {
                return "warning-row";
            }
        },
        showNull(row, column, cellValue) {
            if (cellValue === 0) {
                return "";
            } else {
                return cellValue;
            }
        },
    },

    computed: {
        ...mapGetters({
            userInfo: "user/userInfo",
        }),
    },

    mounted() {
        this.processImageURL = MyApplyApi.getProcessImage;
        this.getApplyInfoList();
        this.applyStatus = applyStatus;
    },
};